package org.hadoop;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.Test;

import java.io.DataInputStream;
import java.io.OutputStream;

public class Demo04WriteFile {
    @Test
    public void testWriteFile() throws Exception {
        String server = "hdfs://192.168.56.201:8020";
        System.setProperty("HADOOP_USER_NAME", "hadoop");
        System.setProperty("HADOOP_HOME", "D:/program/hadoop-3.2.2");
        System.setProperty("hadoop.home.dir", "D:/program/hadoop-3.2.2");
        Configuration config = new Configuration();
        config.set("fs.defaultFS", server);
        try (FileSystem fs = FileSystem.get(config)) {
            OutputStream out = fs.create(new Path(server+"/test/b.txt"));
            out.write("Hello Hadoop\n".getBytes());
            out.write("中文写入测试\n".getBytes());
            out.close();
        }
    }
}
